﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Core.DataAccess.IRepositories;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(IStatusChatRepository))]
    public class StatusChatRepository : IStatusChatRepository
    {
        private Connection conn;

        public StatusChatRepository()
        {
            conn = new Connection();
        }

        public void SaveStatus(Status status)
        {
            using (MXHEntities context = conn.GetContext())
            {
                if (status.StatusID > 0)
                {
                    context.Status.Attach(new Status { StatusID = status.StatusID });
                    context.Status.ApplyCurrentValues(status);
                }
                else
                {
                    context.Status.AddObject(status);
                }
                context.SaveChanges();
            }
        }

        public void UpdateStatusChat(int accountID, bool s)
        {
            using (MXHEntities context = conn.GetContext())
            {
                Status status = (from st in context.Status
                                 where st.AccountID == accountID
                                 select st).FirstOrDefault();
                if (status != null)
                {
                    status.AccountStatus = s;
                    context.SaveChanges();
                }
            }
        }

        public Status GetStatusByAccountID(int accountID)
        {
            Status status = null;

            using (MXHEntities context = conn.GetContext())
            {
                var stt = from st in context.Status
                          where st.AccountID == accountID
                          select st;
                if (stt != null)
                    status = stt.FirstOrDefault();
            }
            return status;
        }
    }
}
